package de.tewdreyer.android.erc;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

public class ERC extends TabActivity {
    /**
     * @see android.widget.TabHost.OnTabChangeListener 
     */
	private class ShowInfoOnTabChangeListener implements OnTabChangeListener {
		/**
		 * reload webview if changed to tab displaying the device's infos 
		 * @param tabId name of tab changed to
		 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(String)
		 */
		@Override
		public void onTabChanged(String tabId){
			if(tabId == "showInfo")
    			ShowInfo.reload();
    	}
	}
	
	/**
	 * device information
	 * use deviceMacher to check integrity of deviceIdentifier 
	 */
	public static String deviceIdentifier;
    public static String deviceMatcher = "MOGE\\d{11}"; 
    
    /**
     * urls and parameter
     */
    public static String logMessageUrl = "http://twiki.molgen.mpg.de/foswiki/bin/save_tobi/Medv/InvReg";
    public static String showInfoUrl = "http://twiki.molgen.mpg.de/foswiki/Medv/InvReg";
    public static String uploadPhotoUrl = "http://twiki.molgen.mpg.de/foswiki/bin/upload_tobi/Medv/InvReg";
    public static String urlParamShowPlain = "skin=plain";
    
    /**
     * use to identify intent
     */
    private final int READ_DEVICEID = 0; 
    
    /**
     * create visual objects (tabs)
     * @param inState activity's previous state 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle inState)
    {
        super.onCreate(inState);

        setContentView(R.layout.main);

        if(inState == null){
            getDeviceIdentifier();
        }
        
        // create visible tabs
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        // ShowInfo tab
        intent = new Intent().setClass(getApplicationContext(),ShowInfo.class);
        spec = tabHost.newTabSpec("showInfo");
        spec.setIndicator("Show Info",getResources().getDrawable(android.R.drawable.ic_menu_compass));
        spec.setContent(intent);
        tabHost.addTab(spec);

        // LogMessage tab
        intent = new Intent().setClass(getApplicationContext(), LogMessage.class);
        spec = tabHost.newTabSpec("logMessage");
        spec.setIndicator("Log Message",getResources().getDrawable(android.R.drawable.ic_menu_edit));
        spec.setContent(intent);
        tabHost.addTab(spec);

        // UploadPhoto tab
        intent = new Intent().setClass(getApplicationContext(), UploadPhoto.class);
        spec = tabHost.newTabSpec("uploadPhoto");
        spec.setIndicator("Upload Photo",getResources().getDrawable(android.R.drawable.ic_menu_gallery));
        spec.setContent(intent);
        tabHost.addTab(spec);
        
        // set onTabChangeListener to reload webview if changed to this tab
        tabHost.setOnTabChangedListener(new ShowInfoOnTabChangeListener());
    }

    /**
     * handle returned device identifier
     * @param requestCode send on activity start
     * @param resultCode  returned by called activity
     * @param result      intent containing send data
     * @see android.app.Activity#onActivityResult(int,int,android.content.Intent)
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent result){
    	switch(requestCode) {
    	case READ_DEVICEID:
    		if(resultCode == RESULT_OK){
    			// set device identifier
    			deviceIdentifier = result.getStringExtra("deviceidentifier");
    			if(!deviceIdentifier.matches(deviceMatcher)){
    				Toast.makeText(getApplicationContext(), "invalid device identifier", Toast.LENGTH_SHORT).show();
    				deviceIdentifier = null;
    			}
    				

    			// reload webview
    			ShowInfo.reload();
    		}
    		break;
    	}
    }

    /** 
     * create options menu
     * @param menu 
     * @return true
     * @see android.app.Activity#onCreateOptionsMenu(Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    /** 
     * handles selection of options menu items
     * @param item menuitem selected 
     * @return true 
     * @see android.app.Activity#onOptionsItemSelected(MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case R.id.newDevice:
    		getDeviceIdentifier();
    		break;
    	}
    	return true;
    }    

    /**
     * new activity is started in order to get a device's identifier
     */
    private void getDeviceIdentifier(){
        Intent readDeviceIdentifier = new Intent();
        readDeviceIdentifier.setAction("android.intent.action.READ");
        readDeviceIdentifier.setType("vnd.tewdreyer.android.erc/vnd.tewdreyer.android.erc.deviceidentifier");
        startActivityForResult(readDeviceIdentifier,READ_DEVICEID);
    }
}
